<?php
ob_start();
session_start();
$_SESSION['aberta'] = true;
?>
<div id="content">    
    <h3 style="padding-bottom:20px">Preencha o formulário completo:</h3>
    <form action="../controller/realizaVenda.php" method="post">
        <table width="350" align="center" cellpadding="2" cellspacing="2">
            <tr>
                <td width="167" align="center">Funcionario </td>
                <td width="167">
                    <input type="text" value="<?php echo $_SESSION['usuarioNome']; ?>" disabled name="funcionario">
                    <input type="hidden" value="<?php echo $_SESSION['usuarioID']; ?>" name="funcionario_id">                    
                </td>
            </tr>
            <tr>
                <td align="center">Data da Compra</td>
                <td>
                    <input type="text" readonly value="<?php echo $_POST['data']; ?>" name="data" id="dataID">
                </td>
            </tr>
            <tr>

                <td align="center" height="50px" colspan="2">Medicamentos 
                </td>
            </tr>
        </table>

        <table  class="tabelaGrid" align="center">   
            <tr class='menuGrid'><td class='menuGrid'>Nome</td><td class='menuGrid'>Quantidade</td><td class='menuGrid'>Valor Unitário</td><td class='menuGrid'>Valor Total</td></tr>            
            <?php
            include("../model/medicamento.php");
            $medicamentos = Medicamento::FindAll();
            $selecionado = FALSE;
            $btn_submit = 0;
            $i = 1;
            $total = 0;
            foreach ($_POST as $key => $value) {
                if ($selecionado && (int) $value > 0) {
                    $Medicamento = Medicamento::medById($key, $medicamentos);
                    $total += $Medicamento->getPreco() * (int) $value;
                    if ($Medicamento->getEstoque() - $value < 0) {
                        echo "<tr><input type='hidden' name='medicamento_id" . $i . "' value='$key'><td>";
                        echo "<input style='background-color:red; font-weight:bold' type='text' disabled name='nome" . $i . "' value='" . $Medicamento->getName() . "'/>";
                        echo "</td><td>";
                        echo "<input style='background-color:red; font-weight:bold' class='readonly' type='text' readonly size='4' name='qtde" . $i . "' value='" . (int) $value . "'/>";
                        echo "</td><td>";
                        echo "<input type='text' class='readonly' readonly size='4' name='valUni" . $i . "' value='" . $Medicamento->getPreco() . "'/>";
                        echo "</td><td>";
                        echo "<input type='text' disabled size='4'name='valTotal" . $i . "' value='" . $Medicamento->getPreco() * (int) $value . "'/>";
                        echo "</td></tr>";
                        $btn_submit = 1;
                    } else {
                        echo "<tr><input type='hidden' name='medicamento_id" . $i . "' value='$key'><td>";
                        echo "<input type='text' disabled name='nome" . $i . "' value='" . $Medicamento->getName() . "'/>";
                        echo "</td><td>";
                        echo "<input class='readonly' type='text' readonly size='4' name='qtde" . $i . "' value='" . (int) $value . "'/>";
                        echo "</td><td>";
                        echo "<input type='text' class='readonly' readonly size='4' name='valUni" . $i . "' value='" . $Medicamento->getPreco() . "'/>";
                        echo "</td><td>";
                        echo "<input type='text' disabled size='4'name='valTotal" . $i . "' value='" . $Medicamento->getPreco() * (int) $value . "'/>";
                        echo "</td></tr>";
                    }
                    $selecionado = FALSE;
                    $i++;
                }
                if ($key != "data" && (float) $key == 0 && $value == "on") {
                    $selecionado = TRUE;
                }
            }
            if ($i == 1) {
                $btn_submit = 1;
                echo "<tr id='tabelaVazia'><td></td></tr>";
                echo "<h4> Nenhum medicamento inserido! </h4>";
            }
            ?>            
        </table>
        <?php
        if ($i > 1) {
            echo "<input type='text' style='margin-left:330px; margin-top: 5px' align='center' readonly size='4' name='valTotalT' value='" . $total . "'>";
            if ($btn_submit == 1)
                echo "<br/>Os medicamentos em vermelho ultrapassam o limite em estoque.";
        }
        ?>
        <br/>
        <?php 
             if ($btn_submit == 1)
                 echo  '<input type="button" onclick="goBack()" value="Voltar">';
             else
                 echo  '<input type="submit" value="Confirmar Venda">';
        ?>        
    </form>
</div>

<?php
$pagemaincontent = ob_get_contents();
ob_end_clean();
$pagetitle = "Cadastrar Funcionário";
$javascript = '    
    function onLoad(){
        if(document.getElementById("tabelaVazia") != null) {            
            document.getElementById("tabelaVazia").parentNode.parentNode.style.display = "none";
        }
    }
    function goBack()
    {
            window.history.back();
    }';
$onLoad = "onLoad()";
include("master.php");
?>